	
clear all
set more off, perm

/* Directories */
global orig_data_thecb "E:\"
global orig_data "E:\"
global orig_data_tea "E:\"

global work_dir  "E:\projects\Rank\workfiles\"
global ui_dir "E:\projects\Rank\temp\"
global logfiles "E:\projects\Rank\logfiles\"
global dofiles "E:\projects\Rank\dofiles\"
global results "E:\projects\Rank\results\"


********************************************************************************
*APPENDIX Table 4 - School Choice 
********************************************************************************

********************************************************************************
******************************GOOD BAD SCHOOLS**********************************
********************************************************************************
encode campus_3, gen(sch_3)

*Unconditional FX
reghdfe state_perc_8, absorb(sch_3, savefe)
rename __hdfe1__ sch_fx
*Value Added FX
reghdfe state_perc_8 c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all, absorb(sch_3, savefe) 
rename __hdfe1__ sch_fx_va
*Conditional Value Added FX
reghdfe state_perc_8 c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all $controls, absorb(sch_3, savefe) 
rename __hdfe1__ sch_fx_cond_va

cap file close myfile
cd "$results"
file open myfile using good_bad_SCH.csv, write text replace

local tol=0.0045

set more off 
file write myfile "SCH FX" _n
file write myfile ",Mean, Standard Deviation, Observations" _n
sum sch_fx,
file write myfile ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
* +/-0.0025 - Gives 0.69-1.63% for each group
* +/-0.0045 - Gives 1.39-3.28% for each group
gen average_sch=(sch_fx>=r(mean)-`tol') & (sch_fx<=r(mean)+`tol')
gen good_sch=(sch_fx>=r(mean)+r(sd)-`tol') & (sch_fx<=r(mean)+r(sd)+`tol')
gen bad_sch=(sch_fx>=r(mean)-r(sd)-`tol') & (sch_fx<=r(mean)-r(sd)+`tol')

file write myfile ", Average Class, Good Class, Bad Class" _n
sum sch_fx if average_sch==1
local av=r(mean)
sum sch_fx if good_sch==1
local good=r(mean)
sum sch_fx if bad_sch==1
local bad=r(mean)
file write myfile ","  %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n

*Mean Class Rank for each ventile
file write myfile "Mean Class Rank for Ventile" _n
forvalues x=1/20{
di "Ventile `x'"
*Average Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & average_sch==1
local av=r(mean)
*Good Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & good_sch==1
local good=r(mean)
*Bad Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & bad_sch==1
local bad=r(mean)
file write myfile %6.0f (`x') "," %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n
}

 
file write myfile "sch VA FX" _n
file write myfile ",Mean, Standard Deviation, Observations" _n

sum sch_fx_va
file write myfile  ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
gen average_sch_va=(sch_fx_va>=r(mean)-`tol') & (sch_fx_va<=r(mean)+`tol')
gen good_sch_va=(sch_fx_va>=r(mean)+r(sd)-`tol') & (sch_fx_va<=r(mean)+r(sd)+`tol')
gen bad_sch_va=(sch_fx_va>=r(mean)-r(sd)-`tol') & (sch_fx_va<=r(mean)-r(sd)+`tol')
file write myfile ", Average Class, Good Class, Bad Class" _n

sum sch_fx_va if average_sch_va==1
local av=r(mean)
sum sch_fx_va if good_sch_va==1
local good=r(mean)
sum sch_fx_va if bad_sch_va==1
local bad=r(mean)
file write myfile ","  %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n

*Mean Class Rank for each ventile
file write myfile "Mean Class Rank for Ventile" _n
forvalues x=1/20{
di "Ventile `x'"
*Average Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & average_sch_va==1
local av=r(mean)
*Good Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & good_sch_va==1
local good=r(mean)
*Bad Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & bad_sch_va==1
local bad=r(mean)
file write myfile %6.0f (`x') "," %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n
}




file write myfile "sch Conditional VA FX" _n
file write myfile ",Mean, Standard Deviation, Observations" _n

sum sch_fx_cond_va
file write myfile ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
gen average_sch_cond_va=(sch_fx_cond_va>=r(mean)-`tol') & (sch_fx_cond_va<=r(mean)+`tol')
gen good_sch_cond_va=(sch_fx_cond_va>=r(mean)+r(sd)-`tol') & (sch_fx_cond_va<=r(mean)+r(sd)+`tol')
gen bad_sch_cond_va=(sch_fx_cond_va>=r(mean)-r(sd)-`tol') & (sch_fx_cond_va<=r(mean)-r(sd)+`tol')
file write myfile ", Average Class, Good Class, Bad Class" _n

sum sch_fx_cond_va if average_sch_cond_va==1
local av=r(mean)
sum sch_fx_cond_va if good_sch_cond_va==1
local good=r(mean)
sum sch_fx_cond_va if bad_sch_cond_va==1
local bad=r(mean)
file write myfile ","  %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n

*Mean Class Rank for each ventile
file write myfile "Mean Class Rank for Ventile" _n
forvalues x=1/20{
di "Ventile `x'"
*Average Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & average_sch_cond_va==1
local av=r(mean)
*Good Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & good_sch_cond_va==1
local good=r(mean)
*Bad Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & bad_sch_cond_va==1
local bad=r(mean)
file write myfile %6.0f (`x') "," %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n
}



cap file close myfile 


********************************************************************************
***************************3rd Grade School VA**********************************
********************************************************************************

cap encode campus_3, gen(sch_3)

*Unconditional FX
reghdfe st_prank_3_all, absorb(sch_3, savefe)
rename __hdfe1__ sch_fx
*Value Added FX
*reghdfe state_perc_8 c.st_prank_3_all##c.st_prank_3_all##c.st_prank_3_all, absorb(sch_3, savefe) 
reghdfe state_perc_8 c.st_prank_3_all, absorb(sch_3, savefe) 
rename __hdfe1__ sch_fx_va


cap file close myfile
cd "$results"
file open myfile using good_bad_3g_linear_SCH.csv, write text replace

local tol=0.0045

set more off 
file write myfile "SCH FX" _n
file write myfile ",Mean, Standard Deviation, Observations" _n
sum sch_fx,
file write myfile ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n

gen average_sch=(sch_fx>=r(mean)-`tol') & (sch_fx<=r(mean)+`tol')
gen good_sch=(sch_fx>=r(mean)+r(sd)-`tol') & (sch_fx<=r(mean)+r(sd)+`tol')
gen bad_sch=(sch_fx>=r(mean)-r(sd)-`tol') & (sch_fx<=r(mean)-r(sd)+`tol')

file write myfile ", Average Class, Good Class, Bad Class" _n
sum sch_fx if average_sch==1
local av=r(mean)
sum sch_fx if good_sch==1
local good=r(mean)
sum sch_fx if bad_sch==1
local bad=r(mean)
file write myfile ","  %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n

*Mean Class Rank for each ventile
file write myfile "Mean Class Rank for Ventile" _n
forvalues x=1/20{
di "Ventile `x'"
*Average Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & average_sch==1
local av=r(mean)
*Good Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & good_sch==1
local good=r(mean)
*Bad Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & bad_sch==1
local bad=r(mean)
file write myfile %6.0f (`x') "," %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n
}

 
file write myfile "sch VA FX" _n
file write myfile ",Mean, Standard Deviation, Observations" _n

sum sch_fx_va
file write myfile  ","  %6.3f (r(mean)) "," %6.3f (r(sd)) "," %12.0f (r(N)) "," _n
gen average_sch_va=(sch_fx_va>=r(mean)-`tol') & (sch_fx_va<=r(mean)+`tol')
gen good_sch_va=(sch_fx_va>=r(mean)+r(sd)-`tol') & (sch_fx_va<=r(mean)+r(sd)+`tol')
gen bad_sch_va=(sch_fx_va>=r(mean)-r(sd)-`tol') & (sch_fx_va<=r(mean)-r(sd)+`tol')
file write myfile ", Average Class, Good Class, Bad Class" _n

sum sch_fx_va if average_sch_va==1
local av=r(mean)
sum sch_fx_va if good_sch_va==1
local good=r(mean)
sum sch_fx_va if bad_sch_va==1
local bad=r(mean)
file write myfile ","  %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n

*Mean Class Rank for each ventile
file write myfile "Mean Class Rank for Ventile" _n
forvalues x=1/20{
di "Ventile `x'"
*Average Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & average_sch_va==1
local av=r(mean)
*Good Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & good_sch_va==1
local good=r(mean)
*Bad Schools
sum cl_prank_3_all if  vent_st_prank_all_`x'==1 & bad_sch_va==1
local bad=r(mean)
file write myfile %6.0f (`x') "," %6.3f (`av') "," %6.3f (`good') ","  %6.3f (`bad') "," _n
}


cap file close myfile 


